package web;
import pojo.SmbmsBill;
import pojo.SmbmsUser;
import service.SmbmsBillService;
import service.SmbmsUserService;
import service.impl.SmbmsUserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
@WebServlet(value = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        this.doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //获取页面的值
        String userCode=req.getParameter("userCode");
        String pwd=req.getParameter("password");
        //调用业务层代码
        SmbmsUserService smbmsUserService=new SmbmsUserServiceImpl();
        try {
            SmbmsUser login = smbmsUserService.login(userCode, pwd);
            if(null !=login){
                //获取session
                HttpSession session=req.getSession();//一次会话
                session.setAttribute("userSession",login);
                SmbmsUser smbmsUser=new SmbmsUser();
                SmbmsUser userSession = (SmbmsUser) session.getAttribute("userSession");
                smbmsUser.setCreatedBy(userSession.getId());
                //转发
                req.getRequestDispatcher("welcome.jsp").forward(req,resp);
            }else{
                resp.sendRedirect("login.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
